<template>
  <el-dialog v-dialogDrag :visible.sync="dialogVisible" :title="title" width="850px" @close="resetForm('ruleForm')">
    <button-group ref="buttonGroup" @submitForm="submitForm('ruleForm')" @cancel="dialogVisible = false" />
    <el-row>
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="设备名称" prop="equipName">
              <el-input v-model.trim="ruleForm.equipName" maxlength="20" show-word-limit />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="设备编号" prop="equipCode">
              <el-input v-model.trim="ruleForm.equipCode" maxlength="20" show-word-limit />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="规格型号" prop="model">
              <el-input v-model="ruleForm.model" maxlength="20" show-word-limit />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="设备类型" prop="type">
              <el-input v-model.trim="ruleForm.type" maxlength="20" show-word-limit />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="使用状况" prop="usageCondition">
              <el-input v-model.trim="ruleForm.usageCondition" maxlength="20" show-word-limit />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="安装地点" prop="location">
              <el-input v-model.trim="ruleForm.location" maxlength="20" show-word-limit />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="使用部门" prop="deptId">
              <tree-select ref="treeSelect" style="width: 100%" :props="treeProps" :options="departData" :disabled="false" :accordion="true" :value="ruleForm.deptId" search-key="deptName" @getNode="getValue($event)" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="负责人" prop="userId">
              <el-select v-model="ruleForm.userId" style="width: 100%">
                <el-option v-for="item in userAllList" :key="item.id" :label="item.name" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="购置时间" prop="purchaseDate">
              <el-date-picker v-model="ruleForm.purchaseDate" type="datetime" format="yyyy-MM-dd" value-format="yyyy-MM-dd" placeholder="选择日期时间"></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="ruleForm.remark" type="textarea" rows="3" maxlength="500" show-word-limit />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-row>
  </el-dialog>
</template>
<script>
import TreeSelect from '@/components/TreeSelect'
export default {
  components: { TreeSelect },
  props: {
    userAllList: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data() {
    const checkName = async (rule, value, callback) => {
      const res = await this.$axios.get(
        `/basic/equip/verifyNameUnique?equipName=${value}&id=${
          this.ruleForm.id || ''
        }`
      )
      if (res?.data.code === 200) {
        return res.data.data
          ? callback()
          : new Error(callback('设备名称重复！'))
      } else {
        return new Error(callback(res.data.msg))
      }
    }
    return {
      title: '',
      deptList: [],
      ruleForm: {
        equipName: '',
        equipCode: '',
        model: '',
        remark: '',
        location: '',
        deptId: '',
        purchaseDate: '',
        type: '',
        usageCondition: '',
        userId: ''
      },
      treeProps: {
        value: 'id',
        label: 'deptName',
        children: 'children'
      },
      rules: {
        equipName: [
          { required: true, message: '设备名称不能为空！', trigger: 'blur' },
          { validator: checkName, trigger: 'blur' }
        ],
        equipCode: [
          { required: true, message: '设备编号不能为空！', trigger: 'blur' }
        ]
      },
      dialogVisible: false,
      departData: []
    }
  },
  watch: {},
  mounted() {},
  methods: {
    /**
     * @description: 新增编辑
     * @param {*} row
     * @return {*}
     */
    async showDialog(row) {
      this.dialogVisible = true
      await this.getDepartData()
      if (row && row.hasOwnProperty('id')) {
        this.title = '编辑'
        this.ruleForm = Object.assign({}, row)
        console.log(row)
      } else {
        this.title = '新增'
      }
    },
    /**
     * @description: 获取所有部门
     * @return {*}
     */
    async getDepartData() {
      const res = await this.$axios.get('/dept/list')
      this.departData = [{ id: '0', deptName: '无' }, ...res.data.data]
    },
    getValue(val) {
      // console.log(val)
      this.ruleForm.deptId = val.id
      this.ruleForm.eptName = val.deptName
    },
    /**
     * @description: 表单提交
     * @param {*} formName
     * @return {*}
     */
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const res = await this.$axios.post('/basic/equip/save', this.ruleForm)
          if (res?.data.code === 200) {
            this.$notify.success('提交成功！')
            this.dialogVisible = false
            this.$emit('getTableInfo')
          } else {
            this.$notify.error(res.data.msg)
          }
        }
      })
    },
    /**
     * @description: 重置表单
     * @param {*} formName
     * @return {*}
     */
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.ruleForm = {
        equipName: '',
        equipCode: '',
        model: '',
        remark: '',
        location: '',
        deptId: '',
        purchaseDate: '',
        type: '',
        usageCondition: '',
        userId: ''
      }
    }
  }
}
</script>
<style lang='scss' scoped>
</style>
